﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DataSamplerEditor : Form, IComponentEditor
    {
        /// <summary>
        /// 
        /// </summary>
        public DataSamplerEditor()
        {
            InitializeComponent();
        }

        private void DataSamplerEditor_Load(object sender, EventArgs e)
        {

        }

        #region Implementation of IComponentEditor

        private object _ActivityNode;
        private DataSamplerComponent _SamplerComponent;

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        /// <summary></summary>
        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode = value; }
        }

        /// <summary></summary>
        public Type WorkflowComponentType
        {
            get { return typeof(DataSamplerComponent); }
        }

        /// <summary></summary>
        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents, object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._SamplerComponent = (DataSamplerComponent)wfComponent;

            DataSamplerInstruction instruction = (DataSamplerInstruction)this._SamplerComponent.Instruction;
            this.txt_DataSource.Text = instruction.DataSourceName;
            string connStr = this.BuildConnectionString(instruction.DataSourceName);
            this.PopulateDbs(connStr);
            if(instruction.SamplingType==SamplingMethod.Percent)
                this.rd_SamplePercent.Checked = true;
            else
                this.rd_SampleCount.Checked = true;
            this.num_SampleSize.Value = instruction.SampleSize;
            this.txt_OutputFile.Text = instruction.OutputFilePath;
            this.rt_SQL.Text = instruction.Sql;
            this.rd_SampleAll.Checked = true;
            this.rd_SampleField.Checked = false;
            this.cbo_SampleByField.SelectedIndex = -1;
            this.ck_Aggregate.Checked = false;

            this.dv_Fields.Rows.Clear();
            this.dv_FieldAggregates.Rows.Clear();
            this.cbo_AggregateField.Items.Clear();
            this.cbo_SampleByField.Items.Clear();

            if(instruction.TableNames==null || instruction.TableNames.Count==0)
                return;

            foreach(string tblName in instruction.TableNames)
            {
                string[] parts = tblName.Split(new char[] {'.'});
                if(parts.Length !=3)
                    continue;
                string dbName = parts[0];
                string schemaName = parts[1];
                string tblname = parts[2];
                if(instruction.TableFields !=null && instruction.TableFields.ContainsKey(tblName))
                {
                    List<string> fieldNames = instruction.TableFields[tblName];
                    foreach(string fieldName in fieldNames)
                    {
                        DataGridViewRow dr=new DataGridViewRow();
                        DataGridViewTextBoxCell cellDb=new DataGridViewTextBoxCell();
                        cellDb.Value = dbName;
                        dr.Cells.Add(cellDb);
                        DataGridViewTextBoxCell cellSchema=new DataGridViewTextBoxCell();
                        cellSchema.Value = schemaName;
                        dr.Cells.Add(cellSchema);
                        DataGridViewTextBoxCell celltbl=new DataGridViewTextBoxCell();
                        celltbl.Value = tblname;
                        dr.Cells.Add(celltbl);
                        DataGridViewTextBoxCell cellField=new DataGridViewTextBoxCell();
                        cellField.Value = fieldName;
                        dr.Cells.Add(cellField);
                        DataGridViewTextBoxCell cellFilter=new DataGridViewTextBoxCell();
                        cellFilter.Value = string.Empty;
                        if(instruction.FieldFilters !=null && instruction.FieldFilters.ContainsKey(fieldName))
                        {
                            cellFilter.Value = instruction.FieldFilters[fieldName];
                        }
                        dr.Cells.Add(cellFilter);
                        DataGridViewTextBoxCell cellDist=new DataGridViewTextBoxCell();
                        cellDist.Value = string.Empty;
                        if(instruction.FieldDistributions.ContainsKey(fieldName))
                        {
                            string dist = string.Empty;
                            foreach(object val in instruction.FieldDistributions[fieldName].Keys)
                            {
                                if(dist.Length>0)
                                {
                                    dist += ",";
                                }
                                dist += string.Format("{0}={1}", val.ToString(),
                                                      instruction.FieldDistributions[fieldName][val]);
                            }
                            cellDist.Value = dist;
                        }
                        dr.Cells.Add(cellDist);
                        this.dv_Fields.Rows.Add(dr);

                        // aggregate fields
                        DataGridViewRow dr2 = new DataGridViewRow();
                        DataGridViewTextBoxCell cellDb2 = new DataGridViewTextBoxCell();
                        cellDb2.Value = dbName;
                        dr2.Cells.Add(cellDb2);
                        DataGridViewTextBoxCell cellSchema2 = new DataGridViewTextBoxCell();
                        cellSchema2.Value = schemaName;
                        dr2.Cells.Add(cellSchema2);
                        DataGridViewTextBoxCell celltbl2 = new DataGridViewTextBoxCell();
                        celltbl2.Value = tblname;
                        dr2.Cells.Add(celltbl2);
                        DataGridViewTextBoxCell cellField2 = new DataGridViewTextBoxCell();
                        cellField2.Value = fieldName;
                        dr2.Cells.Add(cellField2);
                        DataGridViewTextBoxCell cellAlias=new DataGridViewTextBoxCell();
                        if (instruction.FieldsAlias != null && instruction.FieldsAlias.ContainsKey(fieldName))
                            cellAlias.Value = instruction.FieldsAlias[fieldName];
                        else
                            cellAlias.Value = fieldName;
                        dr2.Cells.Add(cellAlias);
                        DataGridViewComboBoxCell cellAggregate=new DataGridViewComboBoxCell();
                        foreach(AggregateFunction aggr in Enum.GetValues(typeof(AggregateFunction)))
                        {
                            cellAggregate.Items.Add(aggr.ToString());
                        }
                        if (instruction.FieldAggregateFunctions != null && instruction.FieldAggregateFunctions.ContainsKey(fieldName))
                            cellAggregate.Value = instruction.FieldAggregateFunctions[fieldName].ToString();
                        dr2.Cells.Add(cellAggregate);
                        DataGridViewTextBoxCell cellFunc=new DataGridViewTextBoxCell();
                        if (instruction.FieldCustomFunctions != null && instruction.FieldCustomFunctions.ContainsKey(fieldName))
                            cellFunc.Value = instruction.FieldCustomFunctions[fieldName];
                        else
                            cellFunc.Value = string.Empty;
                        dr2.Cells.Add(cellFunc);

                        this.dv_FieldAggregates.Rows.Add(dr2);

                        this.cbo_SampleByField.Items.Add(fieldName);
                        this.cbo_AggregateField.Items.Add(fieldName);
                    }
                }
            }

            if(!string.IsNullOrEmpty(instruction.SampleByField))
            {
                this.rd_SampleAll.Checked = false;
                this.rd_SampleField.Checked = true;
                this.SelectComboxField(this.cbo_SampleByField, instruction.SampleByField);
            }
            if(!string.IsNullOrEmpty(instruction.AggregateField))
            {
                this.ck_Aggregate.Checked = true;
                this.SelectComboxField(this.cbo_AggregateField, instruction.AggregateField);
            }
        }

        private string BuildConnectionString(string dataSrc)
        {
            string connStr = string.Format("Data source={0};database=Master;integrated security=sspi;", dataSrc);
            return connStr;
        }

        private string BuildConnectionString(string dataSrc, string dbName)
        {
            string connStr = string.Format("Data source={0};database={1};integrated security=sspi;", dataSrc, dbName);
            return connStr;
        }

        private void PopulateDbs(string connStr)
        {
            string[] dbNames = DbUtil.GetDatabases(connStr);
            this.cbo_Dbs.Items.Clear();
            if(dbNames !=null && dbNames.Length>0)
            {
                foreach(string dbName in dbNames)
                {
                    this.cbo_Dbs.Items.Add(dbName);
                }
            }
        }

        private void PopulateTables(string connStr)
        {
            this.cbo_Tables.Items.Clear();
            Dictionary<string,List<string>> tblNamesWithSchema = DbUtil.GetTableNamesWithSchemas(DataProviderType.MSSQL, connStr);
            if (tblNamesWithSchema != null && tblNamesWithSchema.Count > 0)
            {
                foreach (string schemaName in tblNamesWithSchema.Keys)
                {
                    foreach (string tblName in tblNamesWithSchema[schemaName])
                        if (!string.IsNullOrEmpty(schemaName))
                            this.cbo_Tables.Items.Add(schemaName + "." + tblName);
                        else
                            this.cbo_Tables.Items.Add(tblName);
                }
            }
        }

        private void PopulateTableFields(string connStr, string schemaName, string tblName)
        {
            this.lb_TableFields.Items.Clear();
            TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, schemaName, tblName);
            if(cols !=null && cols.Length>0)
            {
                foreach(TableColumn col in cols)
                {
                    this.lb_TableFields.Items.Add(col.ColumnName);
                }
            }
        }

        private void SelectComboxField(ComboBox comboBox, string field)
        {
            if (!string.IsNullOrEmpty(field) && comboBox.Items.Count > 0)
            {
                for (int i = 0; i < comboBox.Items.Count; i++)
                {
                    if (comboBox.Items[i].ToString() == field)
                    {
                        comboBox.SelectedIndex = i;
                        break;
                    }
                }
            }
        }

        /// <summary></summary>
        public void SaveSettings()
        {
            DataSamplerInstruction instruction = (DataSamplerInstruction)this._SamplerComponent.Instruction;
            instruction.DataSourceName = this.txt_DataSource.Text;
            instruction.OutputFilePath = this.txt_OutputFile.Text;
            instruction.Sql = this.rt_SQL.Text.Trim();
            instruction.SamplingType = SamplingMethod.Count;
            if(this.rd_SamplePercent.Checked)
                instruction.SamplingType = SamplingMethod.Percent;
            instruction.SampleSize = (int) this.num_SampleSize.Value;
            instruction.SampleByField = string.Empty;
            if(this.rd_SampleField.Checked && this.cbo_SampleByField.SelectedIndex>=0)
                instruction.SampleByField = this.cbo_SampleByField.Text;
            instruction.AggregateField = string.Empty;
            if(this.ck_Aggregate.Checked && this.cbo_AggregateField.SelectedIndex>=0)
                instruction.AggregateField = this.cbo_AggregateField.Text;
            instruction.TableNames=new List<string>();
            instruction.TableFields=new Dictionary<string, List<string>>();
            instruction.FieldFilters=new Dictionary<string, string>();
            instruction.FieldDistributions=new Dictionary<string, Dictionary<object, int>>();
            foreach(DataGridViewRow dr in this.dv_Fields.Rows)
            {
                if(dr.IsNewRow)
                    continue;

                string dbName = dr.Cells[0].Value.ToString();
                string schemaName = dr.Cells[1].Value.ToString();
                string tblName = dr.Cells[2].Value.ToString();
                string tblName2 = dbName + "." + schemaName + "." + tblName;
                if(!instruction.TableNames.Contains(tblName2))
                    instruction.TableNames.Add(tblName2);
                List<string> fieldNames=new List<string>();
                if (instruction.TableFields.ContainsKey(tblName2))
                    fieldNames = instruction.TableFields[tblName2];
                string fieldName = dr.Cells[3].Value.ToString();
                fieldNames.Add(fieldName);
                if (instruction.TableFields.ContainsKey(tblName2))
                    instruction.TableFields[tblName2] = fieldNames;
                else 
                    instruction.TableFields.Add(tblName2, fieldNames);
                string filter = dr.Cells[4].Value.ToString();
                if (!string.IsNullOrEmpty(filter) && !instruction.FieldFilters.ContainsKey(fieldName))
                    instruction.FieldFilters.Add(fieldName, filter);
                string dist = dr.Cells[5].Value.ToString();
                if(!string.IsNullOrEmpty(dist))
                {
                    Dictionary<object,int> valDistributions=new Dictionary<object, int>();
                    string[] pairs = dist.Split(new char[] {','});
                    foreach(string pair in pairs)
                    {
                        string[] valPct = pair.Split(new char[] {'='});
                        if(valPct.Length==2)
                        {
                            if(!valDistributions.ContainsKey(valPct[0]))
                            {
                                int pct = int.Parse(valPct[1]);
                                valDistributions.Add(valPct[0], pct);
                            }
                        }
                    }
                    if(!instruction.FieldDistributions.ContainsKey(fieldName))
                        instruction.FieldDistributions.Add(fieldName, valDistributions);
                }
            }

            instruction.FieldsAlias=new Dictionary<string, string>();
            instruction.FieldAggregateFunctions=new Dictionary<string, AggregateFunction>();
            instruction.FieldCustomFunctions=new Dictionary<string, string>();
            if(!string.IsNullOrEmpty(instruction.AggregateField))
            {
                foreach(DataGridViewRow dr in this.dv_FieldAggregates.Rows)
                {
                    if(dr.IsNewRow)
                        continue;

                    //string dbName = dr.Cells[0].Value.ToString();
                    //string schemaName = dr.Cells[1].Value.ToString();
                    //string tblName = dr.Cells[2].Value.ToString();
                    // string tblName2 = dbName + "." + schemaName + "." + tblName;
                    string fieldName = dr.Cells[3].Value.ToString();
                    string alias = fieldName;
                    if (dr.Cells[4].Value != null)
                        alias = dr.Cells[4].Value.ToString();
                    AggregateFunction aggrFunc = AggregateFunction.Skip;
                    if(dr.Cells[5].Value !=null)
                    {
                        aggrFunc =
                            (AggregateFunction) Enum.Parse(typeof (AggregateFunction), dr.Cells[5].Value.ToString());
                    }
                    string func = string.Empty;
                    if (dr.Cells[6].Value != null)
                        func = dr.Cells[6].Value.ToString();
                    instruction.FieldsAlias.Add(fieldName, alias);
                    instruction.FieldAggregateFunctions.Add(fieldName, aggrFunc);
                    instruction.FieldCustomFunctions.Add(fieldName, func);
                }
            }

            this._SamplerComponent.Instruction = instruction;
        }

        /// <summary></summary>
        public TestResult Test()
        {
            throw new System.NotImplementedException();
        }

        #endregion

        private void rd_SampleCount_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rd_SamplePercent.Checked)
                this.num_SampleSize.Maximum = 100;
            else
                this.num_SampleSize.Maximum = 100000;
        }

        private void rd_SamplePercent_CheckedChanged(object sender, EventArgs e)
        {
            if (this.rd_SamplePercent.Checked)
                this.num_SampleSize.Maximum = 100;
            else
                this.num_SampleSize.Maximum = 100000;
        }

        private void btn_RefreshDB_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.txt_DataSource.Text))
            {
                string connStr = this.BuildConnectionString(this.txt_DataSource.Text);
                this.PopulateDbs(connStr);
            }
        }

        private void btn_RefreshTable_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(this.txt_DataSource.Text) && this.cbo_Dbs.SelectedIndex>=0)
            {
                string dbName = this.cbo_Dbs.SelectedItem.ToString();
                string connStr = this.BuildConnectionString(this.txt_DataSource.Text, dbName);
                this.PopulateTables(connStr);
            }
        }

        private void cbo_Tables_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_Tables.SelectedIndex>=0)
            {
                string dbName = this.cbo_Dbs.SelectedItem.ToString();
                string connStr = this.BuildConnectionString(this.txt_DataSource.Text, dbName);
                string tblName = this.cbo_Tables.SelectedItem.ToString();
                string schemaName = string.Empty;
                string[] pair = tblName.Split(new char[] {'.'});
                if(pair.Length==2)
                {
                    schemaName = pair[0];
                    tblName = pair[1];
                }
                this.PopulateTableFields(connStr,schemaName,tblName);
            }
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if(this.lb_TableFields.SelectedItems.Count>0 && 
                !string.IsNullOrEmpty(this.txt_DataSource.Text) && 
                this.cbo_Dbs.SelectedIndex>=0 && 
                this.cbo_Tables.SelectedIndex>=0)
            {
                string dbName = this.cbo_Dbs.Text;
                string schemaName = string.Empty;
                string tblName = this.cbo_Tables.Text;
                string[] pair = tblName.Split(new char[] {'.'});
                if (pair.Length == 2)
                {
                    schemaName = pair[0];
                    tblName = pair[1];
                }

                foreach(object item in this.lb_TableFields.SelectedItems)
                {
                    string fieldName = item.ToString();
                    DataGridViewRow dr = new DataGridViewRow();
                    DataGridViewTextBoxCell cellDb = new DataGridViewTextBoxCell();
                    cellDb.Value = dbName;
                    dr.Cells.Add(cellDb);
                    DataGridViewTextBoxCell cellSchema = new DataGridViewTextBoxCell();
                    cellSchema.Value = schemaName;
                    dr.Cells.Add(cellSchema);
                    DataGridViewTextBoxCell celltbl = new DataGridViewTextBoxCell();
                    celltbl.Value = tblName;
                    dr.Cells.Add(celltbl);
                    DataGridViewTextBoxCell cellField = new DataGridViewTextBoxCell();
                    cellField.Value = fieldName;
                    dr.Cells.Add(cellField);
                    DataGridViewTextBoxCell cellFilter = new DataGridViewTextBoxCell();
                    cellFilter.Value = string.Empty;
                    dr.Cells.Add(cellFilter);
                    DataGridViewTextBoxCell cellDist = new DataGridViewTextBoxCell();
                    cellDist.Value = string.Empty;
                    dr.Cells.Add(cellDist);
                    this.dv_Fields.Rows.Add(dr);

                    DataGridViewRow dr2 = new DataGridViewRow();
                    DataGridViewTextBoxCell cellDb2 = new DataGridViewTextBoxCell();
                    cellDb2.Value = dbName;
                    dr2.Cells.Add(cellDb2);
                    DataGridViewTextBoxCell cellSchema2 = new DataGridViewTextBoxCell();
                    cellSchema2.Value = schemaName;
                    dr2.Cells.Add(cellSchema2);
                    DataGridViewTextBoxCell celltbl2 = new DataGridViewTextBoxCell();
                    celltbl2.Value = tblName;
                    dr2.Cells.Add(celltbl2);
                    DataGridViewTextBoxCell cellField2 = new DataGridViewTextBoxCell();
                    cellField2.Value = fieldName;
                    dr2.Cells.Add(cellField2);
                    DataGridViewTextBoxCell cellAlias = new DataGridViewTextBoxCell();
                    cellAlias.Value = fieldName;
                    dr2.Cells.Add(cellAlias);
                    DataGridViewComboBoxCell cellAggregate = new DataGridViewComboBoxCell();
                    foreach (AggregateFunction aggr in Enum.GetValues(typeof(AggregateFunction)))
                    {
                        cellAggregate.Items.Add(aggr.ToString());
                    }
                    dr2.Cells.Add(cellAggregate);
                    DataGridViewTextBoxCell cellFunc = new DataGridViewTextBoxCell();
                    cellFunc.Value = string.Empty;
                    dr2.Cells.Add(cellFunc);
                    this.dv_FieldAggregates.Rows.Add(dr2);

                    this.cbo_SampleByField.Items.Add(fieldName);
                    this.cbo_AggregateField.Items.Add(fieldName);
                }
            }
        }

        private void btn_UpdateSelection_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.rt_SQL.Text) && !string.IsNullOrEmpty(this.txt_DataSource.Text) && 
                this.cbo_Dbs.SelectedIndex>=0)
            {
                string dbName = this.cbo_Dbs.SelectedItem.ToString();
                string connStr = this.BuildConnectionString(this.txt_DataSource.Text, dbName);
                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL, connStr, this.rt_SQL.Text);
                if(cols !=null && cols.Length>0)
                {
                    this.dv_Fields.Rows.Clear();
                    this.dv_FieldAggregates.Rows.Clear();
                    this.cbo_SampleByField.Items.Clear();
                    this.cbo_AggregateField.Items.Clear();

                    string schemaName = string.Empty;
                    string tblName = string.Empty;
                    foreach(TableColumn col in cols)
                    {
                        string fieldName = col.ColumnName;
                        DataGridViewRow dr = new DataGridViewRow();
                        DataGridViewTextBoxCell cellDb = new DataGridViewTextBoxCell();
                        cellDb.Value = dbName;
                        dr.Cells.Add(cellDb);
                        DataGridViewTextBoxCell cellSchema = new DataGridViewTextBoxCell();
                        cellSchema.Value = schemaName;
                        dr.Cells.Add(cellSchema);
                        DataGridViewTextBoxCell celltbl = new DataGridViewTextBoxCell();
                        celltbl.Value = tblName;
                        dr.Cells.Add(celltbl);
                        DataGridViewTextBoxCell cellField = new DataGridViewTextBoxCell();
                        cellField.Value = fieldName;
                        dr.Cells.Add(cellField);
                        DataGridViewTextBoxCell cellFilter = new DataGridViewTextBoxCell();
                        cellFilter.Value = string.Empty;
                        dr.Cells.Add(cellFilter);
                        DataGridViewTextBoxCell cellDist = new DataGridViewTextBoxCell();
                        cellDist.Value = string.Empty;
                        dr.Cells.Add(cellDist);
                        this.dv_Fields.Rows.Add(dr);

                        DataGridViewRow dr2 = new DataGridViewRow();
                        DataGridViewTextBoxCell cellDb2 = new DataGridViewTextBoxCell();
                        cellDb2.Value = dbName;
                        dr2.Cells.Add(cellDb2);
                        DataGridViewTextBoxCell cellSchema2 = new DataGridViewTextBoxCell();
                        cellSchema2.Value = schemaName;
                        dr2.Cells.Add(cellSchema2);
                        DataGridViewTextBoxCell celltbl2 = new DataGridViewTextBoxCell();
                        celltbl2.Value = tblName;
                        dr2.Cells.Add(celltbl2);
                        DataGridViewTextBoxCell cellField2 = new DataGridViewTextBoxCell();
                        cellField2.Value = fieldName;
                        dr2.Cells.Add(cellField2);
                        DataGridViewTextBoxCell cellAlias = new DataGridViewTextBoxCell();
                        cellAlias.Value = fieldName;
                        dr2.Cells.Add(cellAlias);
                        DataGridViewComboBoxCell cellAggregate = new DataGridViewComboBoxCell();
                        foreach (AggregateFunction aggr in Enum.GetValues(typeof(AggregateFunction)))
                        {
                            cellAggregate.Items.Add(aggr.ToString());
                        }
                        dr2.Cells.Add(cellAggregate);
                        DataGridViewTextBoxCell cellFunc = new DataGridViewTextBoxCell();
                        cellFunc.Value = string.Empty;
                        dr2.Cells.Add(cellFunc);
                        this.dv_FieldAggregates.Rows.Add(dr2);

                        this.cbo_SampleByField.Items.Add(fieldName);
                        this.cbo_AggregateField.Items.Add(fieldName);
                    }
                }
            }
        }

        private void btn_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg=new SaveFileDialog();
            dlg.Filter = "Excel files(*.xlsx)|*.xlsx|Sample file(*.xml)|*.xml|All files(*.*)|*.*";
            if(dlg.ShowDialog()==DialogResult.OK)
            {
                this.txt_OutputFile.Text = dlg.FileName;
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();

            if (this.UpdateComponentSettingEvent != null)
                this.UpdateComponentSettingEvent(this._ActivityNode, this._SamplerComponent);

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbo_Dbs_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_Dbs.SelectedIndex>=0)
            {
                string dbName = this.cbo_Dbs.SelectedItem.ToString();
                string connStr = this.BuildConnectionString(this.txt_DataSource.Text, dbName);
                this.PopulateTables(connStr);
            }
        }

        private void rd_SampleField_CheckedChanged(object sender, EventArgs e)
        {
            if(this.rd_SampleField.Checked)
                this.cbo_SampleByField.Enabled = true;
            else
                this.cbo_SampleByField.Enabled = false;
        }

        private void ck_Aggregate_CheckedChanged(object sender, EventArgs e)
        {
            if(this.ck_Aggregate.Checked)
            {
                this.cbo_AggregateField.Enabled = true;
                this.dv_FieldAggregates.Enabled = true;
            }
            else
            {
                this.cbo_AggregateField.Enabled = false;
                this.dv_FieldAggregates.Enabled = false;
            }
        }
    }
}
